const tokenController = require('../controller/tokenController')
const errorHelper = require('./errorHelper')

module.exports = async (req, res, next) => {
  const { headers } = req
  try {
    if (!headers['x-token']) {
      throw errorHelper(400, 'not access', 1002)
    }
    const authorizationArray = headers['x-token'].split(' ')
    const token = authorizationArray[1]
    // 根据token的type不同采取不同的处理，主要分为shop user 和manager两大类
    const TokenType = authorizationArray[0]
    let uuId = null
    if (!token) {
      throw errorHelper(400, 'token outdate', 1001)
    }
    uuId = await tokenController.checkToken(token, TokenType)
    req.uuId = uuId
    next()
    return
  } catch (e) {
    next(e)
  }
}
